﻿package com.jianghu.helpers;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.URL;
import java.net.URLConnection;

import com.jianghu.R;

import android.content.Context;
import android.util.Log;

/**
 * A hendler for get server content.
 * @author Jason.Huang
 *
 */
public class ContentUtil {
	
	private CacheManager cacheManager;
	private Context context;
	public ContentUtil(Context context) {
		this.context = context;
		cacheManager = new CacheManager(context);
	}
	
	/**
	 * change the server path to a URL. 
	 * @param path context path
	 * @return
	 * @throws IOException
	 */
	public URL createURL(String path) throws IOException{
		if(! path.startsWith("http://")){
			path = context.getString(R.string.server_address) + path;
		}
		return new URL(path);
	}
	
	public InputStream getURLInputStream(String path) throws IOException{
		return getURLInputStream(createURL(path));
	}
	/**
	 * A URL might target to a different resource categories. Such as images and xml files. So we need to do
	 * is to distinguish between them and handle. If it's a picture that get it on local cache data first.
	 * And get it on remote server 2nd. 
	 * @param url
	 * @return null if network don't active and not cache data in the local
	 * @throws IOException
	 */
	public InputStream getURLInputStream(URL url) throws IOException{
		if(cacheManager.isCached(url)){
			return cacheManager.getCacheData(url);
		}else{
			return getRemoteResource(url);
		}
	}
	
	/**
	 * Open a InputStream for remote server
	 * @param url
	 * @return
	 * @throws IOException can't open InputStream for remote URL or network isn't active.
	 * @see NetWorkStatus
	 */
	private InputStream getRemoteResource(URL url) throws IOException{
		boolean isNetWorkActive = NetWorkStatus.isNetworkAvailable(context);
		if(isNetWorkActive){
			URLConnection conn = url.openConnection();
			conn.connect();
			InputStream is = conn.getInputStream();
			cacheManager.addCacheData(url, is);
			return is;
		}else{
			Log.d("jianghu", JianghuException.NET_WORK_DISACTIVE);
			throw new IOException(JianghuException.NET_WORK_DISACTIVE);
		}
	}
	
	/**
	 * Get Json
	 * @param url
	 * @return
	 * @throws IOException can't open InputStream for remote URL or network isn't active.
	 * @see NetWorkStatus
	 */
	public String getJson(URL url) throws IOException{
		InputStream is = getURLInputStream(url);
		StringBuffer sBuff = new StringBuffer();
		
		BufferedReader buf = new BufferedReader(new InputStreamReader(is, "utf8"));
		String s = null;
		while((s = buf.readLine()) != null){
			sBuff.append(s);
		}
		return sBuff.toString();
	}
	
	public String getJson(String path) throws IOException{
		return getJson(createURL(path));
	}
	
	/**
	 * refresh the cache manager.
	 */
	public void refreshCacheManager(){
		cacheManager.refresh();
	}
}
